﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using org.pdfbox.pdmodel;
using org.pdfbox.pdfparser;
using org.pdfbox.util;
using org.pdfbox.cos;
using org.apache.lucene.document;
using java.io;

namespace PdfLucene
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            PDDocument doc = null;  //
            PDFTextStripper stripper = new PDFTextStripper();
            String s = null;
            System.IO.FileInfo fileInfo;
            //FileInputStream fi;
            org.apache.lucene.document.Document luceneDoc;
            try
            {
                fileInfo = new System.IO.FileInfo("file.pdf");
                doc = PDDocument.load("file.pdf");
                s = stripper.getText(doc); //*Not Resolved [Error Here see below]
                luceneDoc = new Document();
                // Add the path of the file as a pdfStripper named "path".
                // Use a pdfStripper that is indexed (i.e., searchable), but don't
                // tokenize the pdfStripper into words.
                luceneDoc.add(new Field("path", fileInfo.FullName, Field.Store.YES,
                           Field.Index.UN_TOKENIZED));

                // Add the last modified date of the file to a pdfStripper named
                // "modified". Use a pdfStripper that is indexed (i.e., searchable),
                // but don't tokenize the pdfStripper into words.
                luceneDoc.add(new Field("modified",
                                DateTools.timeToString(fileInfo.LastWriteTime.Ticks,
                         DateTools.Resolution.MINUTE),
                                Field.Store.YES, Field.Index.UN_TOKENIZED));

                // Add the contents of the file to a pdfStripper named "contents".
                // Specify a Reader, so that the text of the file is tokenized
                // and indexed, but not stored. Note that FileReader expects
                // the file to be in the system's default encoding. If that's
                // not the case, searching for special characters will fail.
                luceneDoc.add(new Field("contents", s,Field.Store.YES,Field.Index.TOKENIZED));
            }
            catch (Exception g)
            {
                Console.WriteLine(g);
            }
            finally
            {
                doc.close();
            }

            textBox1.Text = s;

            //s = string.Empty;
            ////GRoup 2
            //try
            //{
            //    fi = new FileInputStream(new File("file.pdf"));

            //    PDFParser parser = new PDFParser(fi);
            //    parser.parse();
            //    COSDocument cd = parser.getDocument();
            //    //PDFTextStripper stripper = new PDFTextStripper();
            //    s = stripper.getText(new PDDocument(cd));
            //}
            //catch (Exception ex)
            //{
            //    Console.WriteLine(ex);
            //}
            //textBox2.Text = s;
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {
            DateTime start,end;
            string[] files = DirectoryManager.ExploreDirectory(@"D:\Biblioteca\affectivecomputing");
            List<PDFFile> pdfFiles;
            PDFFile pdfFile;
            if (files != null)
            {
                start = DateTime.Now;
                pdfFiles = PDFManager.GetFilesInfo(files);

                end = DateTime.Now;
                TimeSpan s = end - start;
                System.Console.WriteLine(s.Seconds);
                System.Console.WriteLine(pdfFiles.Count);
                //System.Console.Write(pdfFiles[0].Contents);
                
            }
        }
    }
}
